package com.vincent.suchao.adapter;

import java.util.ArrayList;
import java.util.Calendar;

import android.content.Context;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.vincent.suchao.entity.ZXDT_NewsFeed;
import com.vincent.suchao.lcld.R;
import com.vincent.suchao.lcld.ZXDT_Activity;
import com.vincent.suchao.util.ImageAsyncDownloader;
import com.vincent.suchao.util.ImageAsyncDownloader.LoadBitmapCallback;

public class NewsFeedAdapter extends BaseAdapter {
	private ArrayList<Bitmap> bitmaps = new ArrayList<Bitmap>();
	private ImageAsyncDownloader mImageAsyncDownloader;
	private Context context;
	private LayoutInflater mInflater;
	ArrayList<ZXDT_NewsFeed> mFeeds;

	private final int TYPE_BIG = 0;
	private final int TYPE_NORMAL = TYPE_BIG + 1;
	private final int TYPE_DATE = TYPE_NORMAL + 1;

	public NewsFeedAdapter(Context mcontext, ArrayList<ZXDT_NewsFeed> feeds) {
		super();
		this.context = mcontext;
		this.mInflater = LayoutInflater.from(context);
		this.mFeeds = feeds;
		mImageAsyncDownloader = new ImageAsyncDownloader();
	}

	public void setmFeeds(ArrayList<ZXDT_NewsFeed> mFeeds) {
		this.mFeeds = mFeeds;
	}

	public void addmFeeds(ArrayList<ZXDT_NewsFeed> mFeeds) {
		this.mFeeds.addAll(mFeeds);
	}

	@Override
	public int getCount() {
		return mFeeds == null ? 0 : mFeeds.size();
	}

	@Override
	public Object getItem(int position) {
		return mFeeds == null ? null : mFeeds.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public int getItemViewType(int position) {
		// TODO Auto-generated method stub
		int p = position % 4;
		if (p == 0)
			return TYPE_BIG;
		else if (p == 1 || p == 2)
			return TYPE_NORMAL;
		else if (p == 3)
			return TYPE_DATE;
		else
			return 0;
	}

	@Override
	public int getViewTypeCount() {
		// TODO Auto-generated method stub
		return 3;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		if (mFeeds == null)
			return convertView;
		ZXDT_NewsFeed feed = mFeeds.get(position);
		TypeBigHolder bigHolder = null;
		TypeNormalHolder normalHolder = null;
		TypeDateHolder dateHolder = null;
		int type = getItemViewType(position);
		if (convertView == null) {

			switch (type) {
			case TYPE_BIG:
				convertView = mInflater.inflate(R.layout.inflater_newsfeed_item_big, null);
				bigHolder = new TypeBigHolder();
				bigHolder.tv_tittle_big = (TextView) convertView.findViewById(R.id.tv_tittle_big);
				bigHolder.iv_feed_icon_big = (ImageView) convertView.findViewById(R.id.iv_feed_icon_big);
				bigHolder.ll_news_feed = convertView.findViewById(R.id.ll_news_feed);
				convertView.setTag(bigHolder);
				break;
			case TYPE_NORMAL:
				convertView = mInflater.inflate(R.layout.inflater_newsfeed_item_normal, null);
				normalHolder = new TypeNormalHolder();
				normalHolder.tv_tittle = (TextView) convertView.findViewById(R.id.tv_tittle);
				normalHolder.iv_feed_icon = (ImageView) convertView.findViewById(R.id.iv_feed_icon);
				normalHolder.ll_news_feed = convertView.findViewById(R.id.ll_news_feed);
				convertView.setTag(normalHolder);
				break;
			case TYPE_DATE:
				convertView = mInflater.inflate(R.layout.inflater_newsfeed_item_date, null);
				dateHolder = new TypeDateHolder();
				dateHolder.tv_tittle_date = (TextView) convertView.findViewById(R.id.tv_tittle);
				dateHolder.iv_feed_icon_date = (ImageView) convertView.findViewById(R.id.iv_feed_icon);
				dateHolder.tv_date = (TextView) convertView.findViewById(R.id.tv_date);
				dateHolder.rl_date_root = (RelativeLayout) convertView.findViewById(R.id.rl_date_root);
				dateHolder.ll_news_feed = convertView.findViewById(R.id.ll_news_feed);
				convertView.setTag(dateHolder);
				break;
			default:
				break;
			}

		} else {

			switch (type) {
			case TYPE_BIG:
				bigHolder = (TypeBigHolder) convertView.getTag();
				break;
			case TYPE_NORMAL:
				normalHolder = (TypeNormalHolder) convertView.getTag();
				break;
			case TYPE_DATE:
				dateHolder = (TypeDateHolder) convertView.getTag();
				break;
			}

		}

		switch (type) {
		case TYPE_BIG:

			bigHolder.ll_news_feed.setTag(R.id.ll_news_feed, feed);
			bigHolder.tv_tittle_big.setText(feed.getTitle());
			final Object hashCodeForTag1 = feed.getThumb() + position;
			LoadBitmapCallback imageCallback1 = new LoadBitmapCallback() {
				@Override
				public void imageLoaded(Bitmap bitmap, String imageUrl) {

					AbsListView list = null;
					if (context instanceof ZXDT_Activity) {
						list = ((ZXDT_Activity) context).getList();
					}
					if (list != null) {
						ImageView imageView = (ImageView) (list.findViewWithTag(hashCodeForTag1));
						if (imageView != null && imageView != null && bitmap != null) {
							imageView.setImageBitmap(bitmap);

						}
						if (bitmap != null)
							bitmaps.add(bitmap);
					}
				}
			};

			bigHolder.iv_feed_icon_big.setTag(hashCodeForTag1);
			Bitmap bitmap = mImageAsyncDownloader.loadBitmap(context, feed.getThumb(), imageCallback1, -1, -1);
			if (bitmap != null) {
				bitmaps.add(bitmap);
				bigHolder.iv_feed_icon_big.setImageBitmap(bitmap);
			} else {
				bigHolder.iv_feed_icon_big.setTag(null);
				// default logo set here
				// imageView.setImageResource(R.drawable.xx);
			}
			break;
		case TYPE_NORMAL:
			normalHolder.ll_news_feed.setTag(R.id.ll_news_feed, feed);
			normalHolder.tv_tittle.setText(feed.getTitle());
			final Object hashCodeForTag2 = feed.getThumb() + position;
			LoadBitmapCallback imageCallback2 = new LoadBitmapCallback() {
				@Override
				public void imageLoaded(Bitmap bitmap, String imageUrl) {

					AbsListView list = null;
					if (context instanceof ZXDT_Activity) {
						list = ((ZXDT_Activity) context).getList();
					}
					if (list != null) {
						ImageView imageView = (ImageView) (list.findViewWithTag(hashCodeForTag2));
						if (imageView != null && imageView != null && bitmap != null) {
							imageView.setImageBitmap(bitmap);

						}
						if (bitmap != null)
							bitmaps.add(bitmap);
					}
				}
			};

			normalHolder.iv_feed_icon.setTag(hashCodeForTag2);
			Bitmap bitmap2 = mImageAsyncDownloader.loadBitmap(context, feed.getThumb(), imageCallback2, -1, -1);
			if (bitmap2 != null) {
				bitmaps.add(bitmap2);
				normalHolder.iv_feed_icon.setImageBitmap(bitmap2);
			} else {
				normalHolder.iv_feed_icon.setTag(null);
				// default logo set here
				// imageView.setImageResource(R.drawable.xx);
			}
			break;
		case TYPE_DATE:
			dateHolder.ll_news_feed.setTag(R.id.ll_news_feed, feed);
			dateHolder.tv_tittle_date.setText(feed.getTitle());
			if (position + 1 < getCount() - 1) {
				long now = (long) Float.parseFloat(mFeeds.get(position + 1).getUpdatetime());
				// long start = System.currentTimeMillis();

				// long c = now - start;
				Calendar calendar = Calendar.getInstance();

				calendar.setTimeInMillis(now);

				dateHolder.tv_date.setText(calendar.HOUR_OF_DAY + ":" + calendar.MINUTE);
				dateHolder.rl_date_root.setVisibility(View.VISIBLE);
			} else {
				dateHolder.rl_date_root.setVisibility(View.GONE);
			}
			final Object hashCodeForTag3 = feed.getThumb() + position;
			LoadBitmapCallback imageCallback3 = new LoadBitmapCallback() {
				@Override
				public void imageLoaded(Bitmap bitmap, String imageUrl) {

					AbsListView list = null;
					if (context instanceof ZXDT_Activity) {
						list = ((ZXDT_Activity) context).getList();
					}
					if (list != null) {
						ImageView imageView = (ImageView) (list.findViewWithTag(hashCodeForTag3));
						if (imageView != null && imageView != null && bitmap != null) {
							imageView.setImageBitmap(bitmap);

						}
						if (bitmap != null)
							bitmaps.add(bitmap);
					}
				}
			};

			dateHolder.iv_feed_icon_date.setTag(hashCodeForTag3);
			Bitmap bitmap3 = mImageAsyncDownloader.loadBitmap(context, feed.getThumb(), imageCallback3, -1, -1);
			if (bitmap3 != null) {
				bitmaps.add(bitmap3);
				dateHolder.iv_feed_icon_date.setImageBitmap(bitmap3);
			} else {
				dateHolder.iv_feed_icon_date.setTag(null);
				// default logo set here
				// imageView.setImageResource(R.drawable.xx);
			}
			break;
		}

		return convertView;
	}

	public void recycleBitmaps() {
		mImageAsyncDownloader.shoutdown();

	}

	class TypeNormalHolder {
		TextView tv_tittle;
		ImageView iv_feed_icon;
		View ll_news_feed;
	}

	class TypeBigHolder {
		TextView tv_tittle_big;
		ImageView iv_feed_icon_big;
		View ll_news_feed;
	}

	class TypeDateHolder {
		TextView tv_tittle_date;
		ImageView iv_feed_icon_date;
		TextView tv_date;
		View ll_news_feed;
		RelativeLayout rl_date_root;
	}

}
